package com.fr.ticket;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author 付荣
 * @version 创建时间：2019年4月9日 上午10:38:01
 */
public class SearchList{
	private static final String uri = "https://sec-m.ctrip.com/restapi/soa2/12530/json/ticketSpotSearch?_fxpcqlniredt=09031075110214485266";
	private static final int tryTime = 3;
	final static String productIndex;
	static {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String s = sdf.format(new Date());
		productIndex = "product_" + s;
	}
	public static void handleList(String keyword) {
		SearchListParam param = new SearchListParam();
		param.setKeyword(keyword);
		int totalPage = 150;
		int pageSize = 20;
		for(int i =1 ; i<= totalPage ; i++){
			System.out.println(keyword  + ":第" + i + "次");
			int time = 1;
			param.setPidx(i);
			CloseableHttpResponse response = null;
			try {
				HttpPost httpPost = new HttpPost(uri);
				StringEntity stringEntity = new StringEntity((param.toString()), "utf-8");
				httpPost.setEntity(stringEntity);
				httpPost.setHeader("Content-Type", "application/json");
				response = HttpClientUtil.doRequest(httpPost);
				while(response == null && time < tryTime){
					response = HttpClientUtil.doRequest(httpPost);
					time++;
				}
				if(response == null){
					System.out.println("获取列表页数据失败,参数为：" + param);
					continue;
				}
	            String httpEntityContent = HttpClientUtil.getHttpEntityContent(response);
	            if (StringUtils.isNotBlank(httpEntityContent)) {
	            	JSONObject object = JSON.parseObject(httpEntityContent);
	            	JSONObject data = (JSONObject)object.get("data");
	            	//do something
	            	List<JSONObject> list = (List<JSONObject>) data.get("viewspots");
	            	for(JSONObject o: list){
	            		try{
		            		double price  =  Double.parseDouble(o.get("price").toString());
		            		if (price > 0){
		            			long productId = Long.parseLong(o.get("id").toString()) ;
		            			Map<String, Object> map = ProductDetail.handleProductDetail(productId, 1);
		            			o.putAll(map);
		            		}
		            	 }catch(Exception e){
		            		 System.out.println("价格转化失败" + e);
		            	 }
	            	}
	            	try{
	            		ESUtils.writeIndex(productIndex, productIndex, list);
	            	}catch(Exception e){
	            		System.out.println("产品写入es失败" + e);
	            	}
		            if(i == 1){
		            	 try{
		            		int total =  Integer.parseInt(data.get("total").toString());
		            		if(total > 0){
		            			totalPage = total / pageSize + 1;
		            		}
		            	 }catch(Exception e){
		            		 System.out.println("页码转化失败" + e);
		            	 }
		            }
	            }
			} catch (Exception e) {
				System.err.println("SearchList:" + JSON.toJSONString(e) + keyword  + ":第" + i + "次");
			} finally {
				try {
					if (response != null) {
						response.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
